<?php
    /**
    * Controller.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    /**
    * The Controller class is the core class that puts the modules and the views
    * together to deliver the modules information through the view.
    * @package Atman
    */
    class AT_Controller extends AT_Object
    {
        /**
        * The name of the module we're using.
        * @var string
        */
        private $module;
        
        /**
        * The application of the module to execute.
        * @var string
        */
        private $app;
        
        /**
        * The name of the event of the application.
        * @var string
        */
        private $event;
        
        /**
        * The constructor gathers the authorization method, the view, and the
        * module and validates them.
        * @param string $module The name of the module.
        * @param string $app The name of the application to execute.
        * @param string $event The event of the application to run.
        */
        public function __construct($module, $app, $event)
        {
            parent::__construct();
            
            $this->module = $module;
            $this->app = $app;
            $this->event = $event;
        }
        
        /**
        * Invokes the controller to start the controlling process.
        * @param mixed $options Any options that should be passed to the module.
        * @return void
        */
        public function start($options = NULL)
        {
            $app = $this->loadModule($options);
            $event = $this->event;
            
            if ( $app->getAuth()->authenticate() )
            {
                $app->$event();
                $app->getView()->display();
            }
            else
            {
                header('Location: '.AT_SITE_ROOT.'/users/login');
                exit();
            }
        }
        
        /**
        * Loads the module and checks if the application and event are valid.
        * @param mixed $options Any options that should be passed to the module.
        * @return AT_Module
        */
        private function loadModule($options = NULL)
        {
            $classFile = AT_ROOT.'/mod/'.$this->module.'/'.$this->app.'.php';
            
            if ( file_exists($classFile) )
            {
                include_once $classFile;
                
                $className = $this->app;
                $inst = new $className($options);
                
                if ( AT_Module::isValid($inst) )
                {
                    $ref = new ReflectionClass($className);
                    if ( $ref->hasMethod($this->event) )
                    {
                        return $inst;
                    }
                    else
                    {
                        $msg = 'Invalid event: '.$this->app;
                        $this->reportError($msg, PEAR_LOG_WARNING);
                    }
                }
                else
                {
                    $msg = 'Invalid application specified: '.$className;
                    $this->reportError($msg, PEAR_LOG_WARNING);
                }
            }
            else
            {
                $msg = 'Module file not found: '.$classFile;
                $this->reportError($msg, PEAR_LOG_WARNING);
            }
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
